#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    imgInit();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::imgInit()
{
    Mat imgData = imread("test.jpg");
    cvtColor(imgData,imgData,COLOR_BGR2RGB);
    myImg = imgData;
    myQImg = QImage((const uchar*)(imgData.data),imgData.cols,imgData.rows,QImage::Format_RGB888);
    imgShow();
}

void MainWindow::imgProc()
{//人脸识别
    cv::CascadeClassifier face_detector;
    cv::CascadeClassifier eyes_detector;
    face_detector.load("..\\..\\OpenCV\\haarshare\\haarcascade_frontalface_alt.xml");
    eyes_detector.load("..\\..\\OpenCV\\haarshare\\haarcascade_eye.xml");

    vector<cv::Rect> faces;
    Mat imgSrc = myImg;
    Mat imgGray;
    cvtColor(imgSrc,imgGray,COLOR_BGR2GRAY);
    cv::equalizeHist(imgGray,imgGray);
    face_detector.detectMultiScale(imgGray,faces,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(30,30));

    for (int i=0 ; i<faces.size() ; ++i ) {
        Point center(faces[i].x + faces[i].width/2, faces[i].y+faces[i].height/2);
        cv::ellipse(imgSrc,center,Size(faces[i].width/2,faces[i].height/2),0,0,360,Scalar(255,0,255),2,8,0);
    }

    Mat imgDst = imgSrc;
    myQImg = QImage((const uchar*)(imgDst.data),imgDst.cols,imgDst.rows,QImage::Format_RGB888);
    imgShow();
}

void MainWindow::imgShow()
{
    ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size())));
}


void MainWindow::on_pushButton_clicked()
{
    imgProc();
}

